Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unmarshal error when the tag contains omitempty #15

Merged
merged 2 commits into from
Sep 13, 2023
Merged

fix unmarshal error when the tag contains omitempty #15

merged 2 commits into from
Sep 13, 2023

Conversation

garenchan
Copy link
Contributor

@garenchan garenchan commented Sep 12, 2023

Issue

When the tag contains "omitempty", unmarshaling will result in the corresponding field being empty forever.

$ MYAPP_NAME=test go run main.go                                               
&{Name:}
package main

import (
	"fmt"
	"strings"

	"github.com/iamolegga/enviper"
	"github.com/spf13/viper"
)

type Config struct {
	Name string `mapstructure:"name,omitempty"`
}

func main() {
	e := enviper.New(viper.New())
	e.SetEnvPrefix("MYAPP")
	e.AddConfigPath("/etc/myapp")
	e.SetConfigName("app")

	config := &Config{}
	if err := e.Unmarshal(config); err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", config)
}

enviper_test.go Outdated Show resolved Hide resolved
@iamolegga iamolegga merged commit 66e9666 into iamolegga:master Sep 13, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants